package a5.grammar.productions;

import java.util.LinkedList;
import java.util.List;

import a5.Interpreter;
import a5.common.ProgramCursor;
import a5.common.OrderedTree;
import a5.common.Token;
import a5.environment.Environment;
import a5.grammar.annotations.Grammar;
import a5.grammar.grammars.G1;
import a5.grammar.interfaces.Production;

/**
 * <pre>
 * This class provides a method for interpreting the following production
 * Local -> with Binding
 * 
 * The production simply expects the token "with" followed by a {@link a5.grammar.productions.Binding Binding}.
 * 
 * This production uses recursive decent to interpret and parse.
 * </pre>
 * 
 * @author Jason Campos
 * @version Assignment 5, CS 152 (Programming Paradigms) Fall 2012, Jeffrey Smith
 */
@Grammar(clazz=G1.class)
public class Local implements Production{
	// Production dependency
	private Binding binding;
	
	// Hide constructor
	private Local (){}
	
	/**
	 * Interprets the program being examined by the argument interpreter 
	 * using the defined rules for this production. 
	 * 
	 * This method is extraordinaly simple in that it simply checks for a "with" token 
	 * and then forwards the interpreter to the {@link a5.grammar.productions.Binding#interpret(Interpreter) Binding.interpret()} 
	 * method.
	 * 
	 * @param environment
	 * The {@link a5.environment.Environment Environment} responsible for variable and function maintenance
	 * and scoping. 
	 * 
	 * @param cursor
	 * The {@link a5.common.ProgramCursor ProgramCursor} which contains the {@link a5.common.Token Tokens} 
	 * of the program being interpreted.
	 */
	public void interpret(Environment environment, ProgramCursor cursor){
		cursor.match(environment, "with");
		binding.interpret(environment, cursor);
	}
	
	/**
	 * Recursively parses the program of the argument cursor using the variable/function scoping
	 * policies of the argument environment.
	 * 
	 * @param cursor
	 * The {@link a5.common.ProgramCursor ProgramCursor} which contains the {@link a5.common.Token Tokens} 
	 * of the program being interpreted.
	 * 
	 * @return
	 * An {@link a5.common.OrderedTree OrderedTree<Token>} representing the parse structure of the 
	 * the program of the argument cursor.
	 */
	public OrderedTree<Token> parse(ProgramCursor cursor){
		List<OrderedTree<Token>> children = new LinkedList<OrderedTree<Token>>();
		children.add(new OrderedTree<Token>(new Token(cursor.match("with"))));
		children.add(binding.parse(cursor));
		return new OrderedTree<Token>(new Token(this.getClass().getSimpleName()), children);
	}
	
	/**
	 * @param cursor
	 * The {@link a5.common.ProgramCursor ProgramCursor} which contains the {@link a5.common.Token Tokens} 
	 * of the program being interpreted.
	 * 
	 * @return
	 * {@code true} if the execution point of the program is at a point which indicates that
	 * an Local is a valid path of execution. In other words, if the current point of
	 * execution contains an element which is the first element of this production ("with").
	 */
	public boolean first(ProgramCursor cursor) {
		return cursor.first("with");
	}
}
